package fr.bordeaux1.planingdesetudiants.view;

import java.sql.SQLException;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;

import fr.bordeaux1.planingdesetudiants.model.Cours;
import fr.bordeaux1.planingdesetudiants.model.Ue;

public class DeleteCours extends Composite  {
	private IFenetrePrincipale fenetrePrincipale;
	private Combo listeUE;
	private Combo listeCour;
	DeleteCours(CompositUes arg0, int arg1, IFenetrePrincipale fenetrePrincipale) {
		super(arg0,arg1);
		this.fenetrePrincipale = fenetrePrincipale;
		setLayout(new GridLayout(3, false));

		Label lblNomUe = new Label(this, SWT.NONE);
		lblNomUe.setText("Nom UE");
		new Label(this, SWT.NONE);

		listeUE = new Combo(this, SWT.NONE  | SWT.READ_ONLY);
		java.util.List<Ue> res;
		try {
			res = fenetrePrincipale.getDb().listerUes();

			for (Ue ue : res){
				listeUE.add(ue.getCode() + " " + ue.getLibelle());
				listeUE.setData(ue.getCode() + " " + ue.getLibelle(), ue);
			}
		} catch (SQLException e1) {
			e1.printStackTrace();
		}
		listeUE.addSelectionListener(new SelectionListener() {

			@Override
			public void widgetSelected(SelectionEvent arg0) {
				try {
					listeCour.removeAll();
					Ue ueSelected = (Ue) listeUE.getData(listeUE.getItem(listeUE.getSelectionIndex()));
					System.out.println("ue selected : " + ueSelected);
					java.util.List<Cours> cours = DeleteCours.this.fenetrePrincipale.getDb().listerCours();
					System.out.println("liste cours :"+cours);
					for (Cours cour : cours)
						if (cour.getCodeUe().equals(ueSelected.getCode())){
							listeCour.add(cour.getType());
							listeCour.setData(cour);
						}
				} catch (SQLException e1) {
					e1.printStackTrace();
				}
			}

			@Override
			public void widgetDefaultSelected(SelectionEvent arg0) {

			}
		});
		//gd_combo.widthHint = 276;
		//combo.setLayoutData(gd_combo);
		//GridData gd_combo = new GridData(SWT.FILL, SWT.FILL,true,true, 1, 1);
		GridData gd_combo = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);



		gd_combo.heightHint = 294;
		gd_combo.widthHint = 584;
		this.setLayoutData(gd_combo);



		Label lblIntituleCours = new Label(this, SWT.NONE);
		GridData gd_lblIntituleCours = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
		gd_lblIntituleCours.widthHint = 143;
		lblIntituleCours.setLayoutData(gd_lblIntituleCours);
		lblIntituleCours.setText("Intitulée Cours ");
		new Label(this, SWT.NONE);

		listeCour = new Combo(this, SWT.NONE | SWT.READ_ONLY);
		GridData gd_combo_1 = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
		gd_combo_1.widthHint = 242;
		listeCour.setLayoutData(gd_combo_1);

		Button btnSupprimer = new Button(this, SWT.NONE);
		btnSupprimer.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		btnSupprimer.setText("Supprimer");
		btnSupprimer.addSelectionListener(new SelectionListener() {
			
			@Override
			public void widgetSelected(SelectionEvent arg0) {
				try{
				//int selection = listeCour.getSelectionIndex();
				Cours cours = (Cours) listeCour.getData();
				System.out.println("Cours selected : " + cours);
				DeleteCours.this.fenetrePrincipale.getDb().supprimerCours(cours);
				}catch (SQLException e1) {
					e1.printStackTrace();
				}
				listeUE.deselectAll();
				listeCour.removeAll();
				DeleteCours.this.fenetrePrincipale.modificationApportees();
				DeleteCours.this.fenetrePrincipale.updateStatusBar("Cours supprimé avec succès");
			}
			
			@Override
			public void widgetDefaultSelected(SelectionEvent arg0) {
				
			}
		});

	}
}
